package team.scau.laboratory_repair.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import team.scau.laboratory_repair.common.base.BaseService;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.modules.po.QRepairClassify;
import team.scau.laboratory_repair.modules.po.RepairClassify;
import team.scau.laboratory_repair.modules.repository.RepairClassifyRepository;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 30254
 * creadtedate:2018/9/16
 */
@Service
public class RepairClassifyService extends BaseService<RepairClassify, RepairClassifyRepository> {

    @Autowired
    private RepairClassifyRepository repairClassifyRepository;

    private QRepairClassify qRepairClassify = QRepairClassify.repairClassify;

    private HashMap<Long, RepairClassify> repairClassifyMap = new HashMap<>();

    @Override
    public RepairClassifyRepository getRepository() {
        return repairClassifyRepository;
    }

    @Override
    public EntityPathBase<RepairClassify> getQBase() {
        return qRepairClassify;
    }

    public QueryResults<RepairClassify> listOrByExample(RepairClassify repairClassify, MyPage page) {
        JPAQuery<RepairClassify> jpaQuery = queryFactory.select(qRepairClassify).from(qRepairClassify);
        if (repairClassify != null && repairClassify.getLocationId() != null) {
            jpaQuery = jpaQuery.where(qRepairClassify.locationId.eq(repairClassify.getLocationId()));
        }
        jpaQuery = jpaQuery.where(qRepairClassify.valid.eq(true));
        if (page != null) {
            jpaQuery = jpaQuery.orderBy(page.getSort()).offset(page.getPage()).limit(page.getSize());
        }
        return jpaQuery.fetchResults();

    }

    public RepairClassify getById(Long cId){
        return repairClassifyMap.get(cId);
    }

    @PostConstruct
    @Override
    public void init(){
        super.init();
        List<RepairClassify> all = findAll();
        all.forEach((r)->repairClassifyMap.put(r.getId(), r));
    }

    @Override
    public void save(RepairClassify a) throws Exception {
        super.save(a);
        repairClassifyMap.put(a.getId(), a);
    }

    @Override
    public void delete(RepairClassify a) throws Exception {
        super.delete(a);
        repairClassifyMap.remove(a.getId());
    }

    @Override
    public void deleteById(Object o) throws Exception {
        super.deleteById(o);
        repairClassifyMap.remove(o);
    }


    public List<String> listStringSorted() {
        return repairClassifyRepository.findInOrders();
    }

}
